package com.itheima.web;

import com.itheima.utils.FileUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
public class UploadController {

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @PostMapping("/upload")
    public String upload(@RequestParam("myFile") MultipartFile multipartFile) throws IOException {
        String path = FileUtil.upload(multipartFile.getInputStream(),multipartFile.getOriginalFilename());
        redisTemplate.opsForSet().add("file:temp-rubbishset",path);
        redisTemplate.opsForValue().set("file:" + path,1,30, TimeUnit.SECONDS);
        return path;
    }

}
